<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="工序名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入工序名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <el-select v-model="queryParams.enabled" placeholder="是否启用" clearable>
          <el-option v-for="dict in dict.type.is_enabled" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-row :gutter="10" class="mb8 top-right-btn small-line-height">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['cm:bsProcess:add']">新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['cm:bsProcess:export']">导出</el-button>
        </el-col> -->
      </el-row>
    </el-form>
    <el-table border stripe v-loading="loading" :data="bsProcessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工序名称" align="center" prop="name" />
      <el-table-column label="文件类型" align="center" prop="fileType" min-width="180" />
      <el-table-column label="是否启用" align="center" prop="enabled" min-width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_enabled" :value="scope.row.enabled" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" min-width="80" />
      <el-table-column label="付款比例(%)" align="center" prop="paymentRatio" min-width="80" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['cm:bsProcess:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cm:bsProcess:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改工序对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="工序名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入工序名称" />
        </el-form-item>
        <el-form-item label="文件类型" prop="fileType">
          <el-button class="mb8 top-right-btn " type="primary" plain size="mini" @click="addFileType">新增</el-button>
          <!-- <el-input v-model="form.fileType" placeholder="请输入文件类型(验收文件、视频、图片、检测表)" /> -->
          <el-table border stripe :data="form.fileTypeList">
            <el-table-column label="名称" align="center" prop="name">
              <template slot-scope="scope">
                <el-input size="mini" v-model="scope.row.name" placeholder="请输入"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-delete"
                  @click="onDelFileType(scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="enabled">
              <el-radio-group v-model="form.enabled">
                <el-radio v-for="dict in dict.type.is_enabled" :key="dict.value" :label="Number(dict.value)">
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input type="number" v-model="form.sort" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="付款比例(%)" prop="range">
          <el-row :gutter="10">
            <el-col :span="10">
              <el-input v-model="form.paymentRatio1" type="Number" placeholder="请输入开始值" />
            </el-col>
            <el-col :span="2" style="text-align: center">-</el-col>
            <el-col :span="10">
              <el-input v-model="form.paymentRatio2" type="Number" placeholder="请输入结束值" />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listBsProcess, getBsProcess, delBsProcess, addBsProcess, updateBsProcess } from "@/api/pl/bsProcess";

export default {
  name: "Process",
  dicts: ["is_enabled"],

  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 工序表格数据
      bsProcessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        enabled: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "工序名称不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        enabled: [
          { required: true, message: "是否启用不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询工序列表 */
    getList () {
      this.loading = true;
      listBsProcess(this.queryParams).then(response => {
        this.bsProcessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        name: undefined,
        fileType: undefined,
        fileTypeList: [],
        enabled: 1,
        remark: undefined,
        sort: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加工序";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getBsProcess(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工序";
        if (this.form.fileType) {
          const arr = this.form.fileType.split(',');
          this.form.fileTypeList = arr.map(item => {
            return { name: item };
          });
        } else {
          this.form.fileTypeList = [];
        }
      });
    },
    addFileType () {
      let flg = false;
      this.form.fileTypeList.forEach(element => {
        if (!element.name) {
          flg = true;
        }
      });
      if (flg) {
        this.$message.warning('请填写完整信息，再新增！');
        return;
      }
      const info = {
        name: ""
      };
      this.form.fileTypeList.push(info);
    },
    onDelFileType (index) {
      this.form.fileTypeList.splice(index, 1);
    },
    /** 提交按钮 */
    submitForm () {
      if(this.form.paymentRatio1 && this.form.paymentRatio2){
        const val1 = parseFloat(this.form.paymentRatio1) || 0;
        const val2 = parseFloat(this.form.paymentRatio2) || 0;
        if (val1 > val2) {
          this.$message.error("付款比例的起始值不能大于结束值");
          return;
        }
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          const arr = this.form.fileTypeList.map(item => item.name);
          this.form.fileType = arr.join(',');
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateBsProcess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBsProcess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工序编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delBsProcess(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('cm/bsProcess/export', {
        ...this.queryParams
      }, `bsProcess_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
